package org.yourscrum.web;

import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.yourscrum.web.story.StoryVO;
import org.yourscrum.service.ThemeService;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Jettro Coenradie
 */
public class AddStoryToThemeController extends SimpleFormController {
    private ThemeService themeService;

    public AddStoryToThemeController(ThemeService themeService) {
        this.themeService = themeService;
        setCommandName("story");
        setCommandClass(StoryVO.class);

    }

    @Override
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(false));
        binder.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, true));
    }

    @Override
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
        long themeId = ServletRequestUtils.getRequiredLongParameter(request, "themeId");
        StoryVO story = new StoryVO();
        return story;
    }

    @Override
    protected void doSubmitAction(Object command) throws Exception {
        StoryVO story = (StoryVO) command;
        this.themeService.addStoryToTheme(story);
    }
}
